package com.itlaobing.data;

import java.io.*;
import java.util.Arrays;

public class ParseWtmpx {

    public static void main(String[] args) throws IOException {
        File fod = new File( "D:/unix/wtmpx" );
        if( fod.exists() && fod.isFile() ) {

            final long size = fod.length(); // 获得文件大小
            final int unit = 372; // 指定每条数据对应的字节数
            long n = size / unit; // 统计记录数

           // PrintWriter out = new PrintWriter("D:/wtmpx.txt");

            InputStream in = new FileInputStream(fod);
            DataInputStream dis = new DataInputStream(in);

            final byte b = 0 ;
            final byte[] nameBytes = new byte[ 32 ];
            final byte[] ipBytes = new byte[ 257 ];

            for (int i = 0; i < n ; i++) {
                Arrays.fill( nameBytes , b );
                Arrays.fill( ipBytes , b );

                dis.read( nameBytes ); // 32
                String name = new String( nameBytes );
                name = name.trim();

                dis.skip( 4 + 32 ); // 36

                int pid = dis.readInt() ; // 4

                short type = dis.readShort() ; // 2

                dis.skip( 2 + 2 + 2 ) ; // 6

                int seconds = dis.readInt(); // 4
                int millis = dis.readInt() ; // 4

                long time = seconds * 1000L + millis ;

                dis.skip( 4 + 20 + 2 ) ; // 26

                dis.read( ipBytes ) ; // 257
                String ip = new String( ipBytes );
                ip = ip.trim();

                dis.skip(1); // 1

                System.out.println( pid + " , " + name + " , " + type + " , " + time + " , " + ip );
            }

            System.out.println( n );

            dis.close();
            in.close();
        }
    }
}
